package com.exam.utils;

import com.exam.utils.constant.CookieConstant;
import com.exam.utils.constant.RedisConstant;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by v
 * Date:2019/1/7
 * Time:16:26
 */
public class RedisUserIdUtil {

    public static String getUserId(HttpServletRequest request, StringRedisTemplate redisTemplate) {

        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie == null) {
            return null;
        }
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
        return tokenValue;
    }

    public static Integer getAdminUserId(HttpServletRequest request, StringRedisTemplate redisTemplate) {
        Cookie cookie = CookieUtil.get(request, CookieConstant.ADMINTOKEN);
        if (cookie == null) {
            return null;
        }
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
        return Integer.valueOf(tokenValue);
    }
}
